<?php

require_once "AIS_DocCommentTagImpl.php";
require_once "AIS_DocCommentTagJsonRestriction.php";
/**
 * This represents a @param tag with additional restrictions tags like @minimum
 * @see AIS_DocCommentTagJsonRestriction
 */
class AIS_DocCommentTagParamWithRestrictions extends AIS_DocCommentTagImpl
{
	public function __construct($line, $nrOfValues)
	{
		parent::__construct($line, $nrOfValues);
	}
	
	/**
	 * @param AIS_DocCommentTagJsonRestriction $tag The Restriction Tag
	 */
	public function addRestriction($tag)
	{
		 $func = $this->restrictions[$tag->getName()];
		 if ($func)
		 {
		 	$this->$func($tag->getValue(0));	
		 }
	}
	
	public function finishRestrictions()
	{
		$this->formulaNumber = $this->finishFormula($this->formulaNumber);
		$this->formulaArray = $this->finishFormula($this->formulaArray);
		$this->formulaString = $this->finishFormula($this->formulaString);
	}
	
	private function finishFormula($formula)
	{
		if (sizeof($formula) == 0)
		{
			return 'return true;';
		}
		else
		{
			return 'return '.join("&&",$formula).';';
		}
		
	}
	/**
	 * Enter description here...
	 *
	 * @return bool
	 */
	public function isOptional()
	{
		return $this->optional;
	}
	/**
	 * Enter description here...
	 *
	 * @return bool
	 */
	public function hasDefault()
	{
		return $this->default !== false;
	}
	/**
	 * Enter description here...
	 *
	 * @return mixed
	 */
	public function getDefault()
	{
		return $this->default;
	}
	
	public function isValidNumber($value)
	{
		return eval($this->formulaNumber);
	}
	
	public function isValidString($value)
	{
		return eval($this->formulaString);
	}
	
	public function isValidArray($value)
	{
		return eval($this->formulaArray);
	}
	/**
	 * Returns all restrictions that were set for this tag
	 *
	 * @return string[]
	 */
	public function getRestrictions()
	{
		return $this->restricionsSet;
	}
	
	private $restrictionsNumber = "";
	private $restrictionsArray = "";
	private $restrictionsString = "";
	private $formulaNumber = array();
	private $formulaArray = array();
	private $formulaString = array();
	private $optional = false;
	private $default = false;

	private $restrictions = array(
		'@optional' => 'setOptional',
		'@minimum' => 'setMinimum',
		'@maximum' => 'setMaximum',
		'@minItems' => 'setMinItems',
		'@maxItems' => 'setMaxItems',
		'@default' => 'setDefault',
		'@pattern' => 'setPattern',
		'@minLength' => 'setMinLength',
		'@maxLength' => 'setMaxLength'
	);
	
	private $restricionsSet = array();
	
	private function setOptional($val)
	{
		$this->optional = true;
		$this->restricionsSet['optional'] = true;
	}
	
	private function setMinimum($val)
	{
		array_push($this->formulaNumber, '($value >= '.$val.')');
		$this->restricionsSet['minimum'] = $val;
	}
	
	private function setMaximum($val)
	{
		array_push($this->formulaNumber, '($value <= '.$val.')');
		$this->restricionsSet['maximum'] = $val;
	}
	
	private function setMinItems($val)
	{
		array_push($this->formulaArray, '(sizeof($value) >= '.$val.')');
		$this->restricionsSet['minItems'] = $val;
	}
	
	private function setMaxItems($val)
	{
		array_push($this->formulaArray, '(sizeof($value) <= '.$val.')');
		$this->restricionsSet['maxItems'] = $val;
	}
	
	private function setPattern($val)
	{
		array_push($this->formulaString, '(preg_match(\''.$val.'\', $value) != 0)');
		$this->restricionsSet['pattern'] = $val;
	}
	
	private function setDefault($val)
	{
		$this->default = $val;
		$this->restricionsSet['default'] = $val;
	}
	
	private function setMinLength($val)
	{
		array_push($this->formulaString, '(strlen($value) >= '.$val.')');
		$this->restricionsSet['minLength'] = $val;
	}
	
	private function setMaxLength($val)
	{
		array_push($this->formulaString, '(strlen($value) <= '.$val.')');
		$this->restricionsSet['maxLength'] = $val;
	}
}

?>